package com.century.sjt.EMChat.ui;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.century.sjt.R;
import com.century.sjt.EMChat.widget.photoview.EasePhotoView;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;


public class DownPopupWindow extends PopupWindow {

    private Context mContext;

    private View mMenuView;

    private LinearLayout layout_Saveimg;
    private LinearLayout layout_Deleteimg;
    private LinearLayout layout_Abolish;

    private ProgressDialog pd;
    private EasePhotoView image;
    private int default_res = R.drawable.ease_default_image;
    private String localFilePath;
    private Bitmap bitmap;
    private boolean isDownloaded;
    private ProgressBar loadLocalPb;
    private Bitmap mBitmap;

    public DownPopupWindow(Activity context, final Bitmap bitmap, final String imgId) {
        super(context);
        this.mContext = context;
        this.mBitmap = bitmap;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.img_app_item, null);

        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
                mMenuView.setOnTouchListener(new OnTouchListener() {

                    public boolean onTouch(View v, MotionEvent event) {

                        int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                        int y=(int) event.getY();
                        if(event.getAction()==MotionEvent.ACTION_UP){
                            if(y<height){
                                dismiss();
                            }
                        }
                        return true;
            }
        });
        layout_Saveimg = (LinearLayout) mMenuView.findViewById(R.id.layou_save);
        layout_Deleteimg = (LinearLayout) mMenuView.findViewById(R.id.layout_delete);
        layout_Abolish = (LinearLayout) mMenuView.findViewById(R.id.layou_abolish);
        layout_Abolish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        layout_Saveimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveImg(mBitmap,imgId);
            }
        });
        layout_Deleteimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


    /**
     * 下载图片
     *
     * @param
     */
    //保存文件
    public void saveImg(Bitmap bitmap,String imgId) {                            //保存用户头像到指定文件夹
        if (!"".equals(imgId) || null!=imgId) {
            String imgURL= Environment.getExternalStorageDirectory() + "/com.century.sjt/SJT_PIC/";
            String imgName = getStringImgName(6)+".jpg";
            File file = new File(imgURL,imgName);
            if (file.exists()) {
                file.delete();
            }
            try {
                if (null==bitmap||"".equals(bitmap)){
                    Toast.makeText(mContext,mContext.getResources().getString(R.string.text_picture_save_fail),Toast.LENGTH_LONG).show();
                }else {
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                    bos.flush();
                    bos.close();
//                TipUtil.showToast("图片保存路径："+Constant.DOWNLOAD_PICTURE_PATH, getActivity(), 1);
                    Toast.makeText(mContext,mContext.getResources().getString(R.string.text_picture_save_path)+imgURL,Toast.LENGTH_LONG).show();
                }
                dismiss();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+imgURL+imgName)));//扫描当前图片

        }
    }

    public static String getStringImgName(int length){

        char[] charArray = new char[length];

        for(int i=0;i<length;i++){

            Random r = new Random();
            int n = r.nextInt(123);
            while(n<48 || (n>57 && n<65) || (n>90 && n <97) || n>122){//(!((n>=48 && n<=57) || (n>=65 && n<=90) && (n>=97 && n<=122))){
                n = r.nextInt(123);
            }
            charArray[i] = (char)n ;
        }

        return String.valueOf(charArray) ;

    }


}
